<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Forpastime <Forpastime@qq.com>
// +----------------------------------------------------------------------
namespace api\tenant_user\controller;

use api\tenant_user\validate\UserValidate;
use cmf\controller\RestBaseController;
use think\Db;
use think\db\Query;
use think\facade\Cache;
use think\facade\Request;
use think\Validate;

class UserController extends RestBaseController
{

    /**
     * 员工分类
     */
    public function staffCategory()
    {
        $category = Db::name('tenant_staff_category')->where('status',1)->select();

        $this->success('请求成功',$category);
    }

    /**
     * 添加员工
     */
    public function add()
    {
        $param = $this->request->param();

        $user = [
            'name'=>$param['name'],
            'social_work_age'=>$param['social_work_age'],
            'tel'=>$param['tel'],
            'professional_title'=>$param['professional_title'],
            'sex'=>$param['sex'],
            'good_at'=>empty($param['good_at'])?'':$param['good_at'],
            'category_id'=>$param['category_id'],
            'create_time'=>time(),
            'tenant_id'=>$param['tenant_id']

        ];

        $photo_urls = $this->request->param('avatar');
        if(!empty($photo_urls)){
            $user['avatar'] = cmf_get_image_preview_url($photo_urls);
        }

        $cert_urls = $this->request->param('cert_urls');
        $certificates='';
        if(!empty($cert_urls)){
            $user['professional_certificates'] = $certificates;
        
        }
         $res = Db::name('tenant_staff')->insertGetId($user);

         if($res){
            $this->success("添加成功");
         }else{
            $this->error("添加失败");
         }
         
    }

    /**
     * 添加用户
     */
    public function addUser()
    {
        $param = $this->request->param();
        $validate = new UserValidate();
        $result = $validate->check($param);
        if(!$result){
            $this->error($validate->getError());
        }

        $shop = Db::name('shop_info')->where('shop_id',$param['shop_id'])->value('id');

        $tenant_user = Db::name('tenant_user')->where('tel',$param['tel'])->where('shop_info_id',$shop)->find();
        if($tenant_user){
            $this->success("用户已存在");
            
        }else{
            $user = [
                'shop_info_id'=>$shop,
                'name'=>$param['name'],
                'age'=>$param['age'],
                'tel'=>$param['tel'],
                'sex'=>empty($param['sex'])?0:$param['sex'],
                'create_time'=>time(),
                'app_user_id'=>$param['app_user_id']
    
            ];
            $res = Db::name('tenant_user')->insertGetId($user);
    
            if($res){
                $this->success("添加成功");
            }else{
                $this->error("添加失败");
            }
        }
    }
}